//
//  YWDownloadManager.m
//  videoTest3
//
//  Created by jun peng on 2022/7/21.
//

#import "YWDownloadManager.h"

#import "YWDownloadStatus.h"
#import "YWDownloadDataTaskStatus.h"

@interface YWDownloadManager ()

//通过YWDownloadStatus的用网上videoURL作为key、找到对应的dataTask
@property(nonatomic,strong)NSMutableDictionary *dataTaskDict;


@end

@implementation YWDownloadManager
  
SingleM(downloadManager)


- (instancetype)init
{
    self = [super init];
    if (self) {
       
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishDownloadNote:) name:YWFinishDownloadNote object:nil];
    }
    return self;
}

-(void)finishDownloadNote:(NSNotification *)notification{
    
    //完成任务就要从字典移除模型
    
    //拿出模型
    YWDownloadStatus *status = notification.userInfo[@"YWDownloadStatusKey"];

    YWDownloadDataTaskStatus *dataTaskStatus = [self.dataTaskDict objectForKey:status.VideoUrl];
    if (dataTaskStatus == nil) {
        return;
    }
    
    [self.dataTaskDict removeObjectForKey:status.VideoUrl];

}


-(NSMutableDictionary *)dataTaskDict{
    if (_dataTaskDict == nil) {
        _dataTaskDict = [NSMutableDictionary dictionary];
    }
    return _dataTaskDict;
}


//创建了一个任务
-(void)createOneDataTaskWithStatus:(YWDownloadStatus *)status{
    
    YWLog(@"%@",status.fullPath);
    
    
    //1.假如单例已经存在这个这个任务了就不再走下面了，避免重新创建任务覆盖了字典
    YWDownloadDataTaskStatus *dataTaskStatus2 = self.dataTaskDict[status.VideoUrl];
    if (dataTaskStatus2 != nil) {
        return;
    }
    
    //2.创建YWDownloadDataTaskStatus、并保存到字典
    YWDownloadDataTaskStatus *dataTaskStatus = [[YWDownloadDataTaskStatus alloc] init];
    dataTaskStatus.status = status;
    [self.dataTaskDict setObject:dataTaskStatus forKey:status.VideoUrl];
    
    
    //不是暂停状态的才能下载、假如你是暂停下载的请手动按继续按钮、同理完成了的都不能开启下载
    if (status.downloadType != YWDownloadType_Pause && status.downloadType != YWDownloadType_Finish) {
        
        //3.开启
        [dataTaskStatus createDataTask];
    }
    
    
}




//创建了多个任务
-(void)createDataTaskWithStatusArray:(NSArray <YWDownloadStatus *>*)statusArray{
    
    for (YWDownloadStatus *status in statusArray) {
        [self createOneDataTaskWithStatus:status];
    }
    
    
}

//删除了一个任务
-(void)removeOneDataTaskWithStatus:(YWDownloadStatus *)status{
    
    YWDownloadDataTaskStatus *dataTaskStatus = [self.dataTaskDict objectForKey:status.VideoUrl];
    if (dataTaskStatus == nil) {
        return;
    }
    
    [dataTaskStatus removeDataTask];
    
    [self.dataTaskDict removeObjectForKey:status.VideoUrl];
    
}

//继续一个任务
-(void)continueOneDataTaskWithStatus:(YWDownloadStatus *)status{
    
    YWDownloadDataTaskStatus *dataTaskStatus = [self.dataTaskDict objectForKey:status.VideoUrl];
    if (dataTaskStatus == nil) {
        return;
    }
    
    [dataTaskStatus continueDataTask];
}

//暂停一个任务
-(void)pauseOneDataTaskWithStatus:(YWDownloadStatus *)status{
    
    YWDownloadDataTaskStatus *dataTaskStatus = [self.dataTaskDict objectForKey:status.VideoUrl];
    if (dataTaskStatus == nil) {
        return;
    }
    
    [dataTaskStatus pauseDataTask];
    
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
